$(function(){

	var resizeInnerLayout = function() {
		if (emailListingLayout && $("#emailListing").is(":visible"))
			emailListingLayout.resizeAll();
		else if (emailEditLayout && $("#emailEdit").is(":visible"))
			emailEditLayout.resizeAll();
	};

	var outerLayout = null
	,	outerLayoutOptions = {
			center__paneSelector:	".outer-center"
		,	north__paneSelector:	".outer-north"
		,	spacing_open:			8	// ALL panes 
		,	spacing_closed:			10	// ALL panes 
		,	north__closable:		false
		,	north__resizable:		false
		,	north__spacing_open:	0
		,	center__onresize:		resizeInnerLayout
		}

	,	emailListingLayout = null
	,	emailListingLayoutOptions = {
			spacing_open:			8	// ALL panes 
		,	spacing_closed:			10	// ALL panes 
		,	west__size:				180
		,	west__minSize:			180
		,	west__maxSize:			500
		,	east__size:				200
		,	east__minSize:			150
		,	east__maxSize:			300 
		,	north__closable:		false
		,	north__resizable:		false
		,	north__spacing_open:	0
		,	west__togglerLength_open:	0
		,	east__togglerLength_open:	0
		,	east__togglerLength_closed:	0
		,	east__initClosed:		true
		,	center__onresize:		"emailListingInnerLayout.resizeAll"
		}

	,	emailListingInnerLayout = null
	,	emailListingInnerLayoutOptions = {
			center__paneSelector:	".inner-center"
		,	south__paneSelector:	".inner-south"
		,	spacing_open:			8 // ALL panes 
		,	spacing_closed:			10 
		,	south__size:			"50%"
		,	south__minSize:			80 
		,	south__maxSize:			"80%" 
		,   initClosed:              false //默认开启
		}


	,	emailEditLayout = null
	,	emailEditLayoutOptions = {
			spacing_open:			0
		,	triggerEventsOnLoad:	true
		,	center__onresize: function (pane, $Pane, state) {
				// Layout will set the TEXTAREA height, but
				// must manually calculate and set the width...
				var
					$TextArea	= $Pane.children('.ui-layout-content')
				,	outerWidth	= state.innerWidth // handle padding on the pane
				,	padding		= 2 * parseInt( $TextArea.css('paddingLeft') )
				,	border		= 2 * parseInt( $TextArea.css('borderLeftWidth') )
				;
				$TextArea.width( outerWidth - padding - border );
			}
		}

	; 
	
	//删除邮件到回收站（不是真正删除）
	var deletemails = function(){
		var mailidarray = new Array();
		$('#listing .trSelected').each( function(){
			mailidarray.push($('[abbr="mailid"]',this).text());
		});
		$.ajax({
		  url: "/deletemails",
		  type: 'POST',
		  traditional: true,
		  dataType: "json",
		  data: {'mailidarray':mailidarray},
		  error: function( data ) {
			confirm("删除失败");
		  },
		  success: function(){
			window.location.reload();
		  },
		});
	};
	
	//标星
	var starmails = function(){
		var mailidarray = new Array();
		$('#listing .trSelected').each( function(){
			mailidarray.push($('[abbr="mailid"]',this).text());
		});
		$.ajax({
		  url: "/starmails",
		  type: 'POST',
		  traditional: true,
		  dataType: "json",
		  data: {'mailidarray':mailidarray},
		  error: function( data ) {
			confirm("删除失败："+data);
		  },
		  success: function(){
			window.location.reload();
		  },
		});
	
	};
	
	//取消标星
	var unstarmails = function(){
		var mailidarray = new Array();
		$('#listing .trSelected').each( function(){
			mailidarray.push($('[abbr="mailid"]',this).text());
		});
		$.ajax({
		  url: "/unstarmails",
		  type: 'POST',
		  traditional: true,
		  dataType: "json",
		  data: {'mailidarray':mailidarray},
		  error: function( data ) {
			confirm("删除失败："+data);
		  },
		  success: function(){
			window.location.reload();
		  },
		});
	
	};
	
	var readheader = function(){
		var mailidarray = new Array();
		$('#listing .trSelected').each( function(){
			mailidarray.push($('[abbr="mailid"]',this).text());
		});
		if(mailidarray.length != 1){
			confirm("注意只能选择一条记录");
		}else{
			$.ajax({
			  url: "/readheader",
			  type: 'POST',
			  traditional: true,
			  dataType: "json",
			  data: {'mailidarray':mailidarray},
			  error: function( data ) {
				confirm("删除失败："+data);
			  },
			  success: function(data){
				$("#emailheader pre").html(data);
			  },
			});
		}
	};
	
	//加载收件箱邮件列表
	var getemails = function(){
		$("#listing").flexigrid({
			url: '/getemails',//获取收件箱邮件数据
			dataType: 'json',
			colModel : [
				{display: 'id', name : 'mailid', width : 250, sortable : true, align: 'left',hide: true},
				{display: '标星', name : 'star', width : 40, sortable : true, align: 'left'},
				{display: '发件人', name : 'sender', width : 180, sortable : true, align: 'left'},
				{display: '主题', name : 'subject', width : 180, sortable : true, align: 'left'},
				{display: '时间', name : 'receivetime', width : 120, sortable : true, align: 'left'},
				{display: '大小', name : 'filesize', width : 80, sortable : true, align: 'left'},
				],
			buttons : [
				{name: '删除', bclass: 'delete', onpress : deletemails},
				{name: '查看信头', bclass: 'edit', onpress : readheader},
				{name: '标星', bclass: 'add', onpress : starmails},
				{name: '取消标星', bclass: 'delete', onpress : unstarmails},
				{separator: true}
				],
			searchitems : [
				{display: '发件人', name : 'sender'},
				{display: '主题', name : 'subject', isdefault: true}
				],
			sortname: "receivetime",
			sortorder: "desc",
			usepager: true,
			useRp: true,
			rp: 15,
			showTableToggleBtn: true,
			width: "100%",
			height: 300
		});
	}
	
	//加载回收站邮件列表
	var getdeletedemails = function(){
		$("#listing").flexigrid({
			url: '/getdeletedemails',//获取回收站邮件数据
			dataType: 'json',
			colModel : [
				{display: 'id', name : 'mailid', width : 250, sortable : true, align: 'left',hide: true},
				{display: '标星', name : 'star', width : 40, sortable : true, align: 'left'},
				{display: '发件人', name : 'sender', width : 180, sortable : true, align: 'left'},
				{display: '主题', name : 'subject', width : 180, sortable : true, align: 'left'},
				{display: '时间', name : 'receivetime', width : 120, sortable : true, align: 'left'},
				{display: '大小', name : 'filesize', width : 80, sortable : true, align: 'left'},
				],
			buttons : [
				{name: '查看信头', bclass: 'edit', onpress : readheader},
				{name: '标星', bclass: 'add', onpress : starmails},
			    {name: '取消标星', bclass: 'delete', onpress : unstarmails},
				{separator: true}
				],
			searchitems : [
				{display: '发件人', name : 'sender'},
				{display: '主题', name : 'subject', isdefault: true}
				],
			sortname: "receivetime",
			sortorder: "desc",
			usepager: true,
			useRp: true,
			rp: 15,
			showTableToggleBtn: true,
			width: "100%",
			height: 300
		});
	}

	$(document).ready(function() {
		
		//使用juqery.layout来加载显示框架，具体用法详见文档
		outerLayout = $('body').layout( outerLayoutOptions ); 
		
		emailListingLayout = $('#emailListing').layout( emailListingLayoutOptions );
		emailListingInnerLayout = $('#emailListing .ui-layout-center').layout( emailListingInnerLayoutOptions );
		$("#emailListingLayout").on('click',function(){
			emailListingLayout.close('west');
		});
		
		//鼠标扫过会点击按钮高亮显示
		$("#droppable li").on({
			'click':function(){
				$(this).css('background-color','#e0ecf9');
				$(this).siblings('li').each(function(){
					$(this).css('background-color','#999');
				});
			},
			'hover':function(){
				$(this).css('background-color','#e0ecf9');
				$(this).siblings('li').each(function(){
					$(this).css('background-color','#999');
				});
			},
		});

		
		//根据li的id跳转到不同的页面
		$("#droppable li").each(function(){
			var id = $(this).attr('id');
			$('#'+id).on('click',function(){
				window.location.pathname = "/"+id;
				windows.location.replace(window.location);
			});
		})
		
		//根据页面路径加载数据
		var pathname = window.location.pathname;
		if(pathname == "/emails"){
			getemails();
		}else if(pathname == "/trash"){
			getdeletedemails();
		}
		
		//高亮当前页面按钮
		var pageid = pathname.replace('/','#');
		$(pageid).css('background-color','#e0ecf9');
			
	}); 




})